package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.dao.MaintainDao;
import com.bed.lovewithpatientbed.pojo.*;
import com.bed.lovewithpatientbed.service.BedService;
import com.bed.lovewithpatientbed.service.EmployeeService;
import com.bed.lovewithpatientbed.service.MaintainService;
import com.bed.lovewithpatientbed.service.MalfunctionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author myp
 * @version 1.0.0
 * @ClassName MaintainServiceImpl.java
 * @Description 维修记录Service的实现类
 */
@Service
@Transactional
public class MaintainServiceImpl implements MaintainService {

    @Resource
    private MaintainDao maintainDao;
    @Resource
    private MalfunctionService malfunctionService;
    @Resource
    private BedService bedService;
    @Resource
    private EmployeeService employeeService;

    /**
     * @title 增加维修记录
     * @description
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public Integer saveMaintain(Maintain maintain) {
        return maintainDao.insertSelective(maintain);
    }
    /**
     * @title 查询维修记录列表
     * @description 条件组合查询，分页查询
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public List<Maintain> selectMaintainList(LayUiPageBean pageBean, Maintain maintain, Date staData, Date endDate) {
        MaintainExample maintainExample=new MaintainExample();
        MaintainExample.Criteria maintainExampleCriteria = maintainExample.createCriteria();
        //根据故障编号查找
        if(maintain.getMalfunctionId() != null && !"".equals(maintain.getMalfunctionId() )){
            maintainExampleCriteria.andMalfunctionIdEqualTo(maintain.getMalfunctionId());
        }
        //根据床的编号查询
        if(maintain.getBedId() != null && !"".equals(maintain.getBedId() )){
            maintainExampleCriteria.andBedIdEqualTo(maintain.getBedId() );
        }
        //根据故障类型编号查找
        if(maintain.getMalfunctionTypeId() != null && !"".equals(maintain.getMalfunctionTypeId() )){
            maintainExampleCriteria.andMalfunctionTypeIdEqualTo(maintain.getMalfunctionTypeId() );
        }
        //根据床的状态编号查询
        if(maintain.getBedStatusId() != null && !"".equals(maintain.getBedStatusId())){
            maintainExampleCriteria.andBedStatusIdEqualTo(maintain.getBedStatusId());
        }
        //根据维修人员的编号查询
        if(maintain.getEmployeeId() != null && !"".equals(maintain.getEmployeeId())){
            maintainExampleCriteria.andEmployeeIdEqualTo(maintain.getEmployeeId());
        }
        //根据维修记录日期查询
        if(staData != null && !"".equals(staData)){
            maintainExampleCriteria.andMaintainDateGreaterThanOrEqualTo(staData);
        }
        if (endDate != null && !"".equals(endDate)){
            maintainExampleCriteria.andMaintainDateLessThanOrEqualTo(endDate);
        }
        //获得查询数量
        Long countRow = maintainDao.countByExample(maintainExample);

        //查询存在的维修记录
        maintainExampleCriteria.andSoftdelEqualTo(1);
        //判断页面信息
        if(pageBean.getLimit() != null
                && !"".equals(pageBean.getLimit() )
                && pageBean.getPage() != null
                && !"".equals(pageBean.getPage())){
            maintainExample.setOffset((pageBean.getPage() - 1) * pageBean.getLimit());
            maintainExample.setLimit(pageBean.getLimit().intValue());
        }
            List<Maintain> maintainList = maintainDao.selectByExample(maintainExample);
        //设置嵌套查询
        for(int i=0;i<maintainList.size();i++){
           maintainList.set(i,setClass(maintainList.get(i)));
        }
        return maintainList;
    }
    /**
     * @title 查询维修记录
     * @description 根据维修记录的编号查找维修记录
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public Maintain selectMaintain(Integer maintainId) {
        Maintain maintain = maintainDao.selectByPrimaryKey(maintainId);
        setClass(maintain);
        return maintain;
    }
    /**
     * @title 修改维修状态
     * @description 
     * @author myp 
     * @updateTime   
     * @throws 
     */
    @Override
    public Integer update(Maintain maintain) {
        maintain.setMaintainState(1);
        return maintainDao.updateByPrimaryKeySelective(maintain);
    }

    @Override
    public Integer getCount(Maintain maintain, Date staData, Date endDate) {
        MaintainExample maintainExample=new MaintainExample();
        MaintainExample.Criteria maintainExampleCriteria = maintainExample.createCriteria();
        //根据故障编号查找
        if(maintain.getMalfunctionId() != null && !"".equals(maintain.getMalfunctionId() )){
            maintainExampleCriteria.andMalfunctionIdEqualTo(maintain.getMalfunctionId());
        }
        //根据床的编号查询
        if(maintain.getBedId() != null && !"".equals(maintain.getBedId() )){
            maintainExampleCriteria.andBedIdEqualTo(maintain.getBedId() );
        }
        //根据故障类型编号查找
        if(maintain.getMalfunctionTypeId() != null && !"".equals(maintain.getMalfunctionTypeId() )){
            maintainExampleCriteria.andMalfunctionTypeIdEqualTo(maintain.getMalfunctionTypeId() );
        }
        //根据床的状态编号查询
        if(maintain.getBedStatusId() != null && !"".equals(maintain.getBedStatusId())){
            maintainExampleCriteria.andBedStatusIdEqualTo(maintain.getBedStatusId());
        }
        //根据维修人员的编号查询
        if(maintain.getEmployeeId() != null && !"".equals(maintain.getEmployeeId())){
            maintainExampleCriteria.andEmployeeIdEqualTo(maintain.getEmployeeId());
        }
        //根据维修记录日期查询
        if(staData != null && !"".equals(staData)){
            maintainExampleCriteria.andMaintainDateGreaterThanOrEqualTo(staData);
        }
        if (endDate != null && !"".equals(endDate)){
            maintainExampleCriteria.andMaintainDateLessThanOrEqualTo(endDate);
        }
        Long count = maintainDao.countByExample(maintainExample);
        return count.intValue();
    }

    /**
     * @title 嵌套查询放入实体类
     * @description 
     * @author myp 
     * @updateTime   
     * @throws 
     */
    public Maintain setClass(Maintain maintain){
        //放置故障记录
        Malfunction malfunction = malfunctionService.selectMalfunction(maintain.getMalfunctionId());
        maintain.setMalfunction(malfunction);
        return maintain;
    }
}
